package com.atguigu.serviceedu.controller;


import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.R;
import com.atguigu.commonutils.vo.UcenterMemberVo;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.atguigu.serviceedu.client.UcenterClient;
import com.atguigu.serviceedu.entity.EduComment;
import com.atguigu.serviceedu.entity.EduTeacher;
import com.atguigu.serviceedu.entity.vo.UcenterMemberPay;
import com.atguigu.serviceedu.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author 若chen
 * @since 2022-04-30
 */
@RestController
@RequestMapping("/eduservice/comment")
@CrossOrigin
public class EduCommentController {

  @Autowired
  private EduCommentService eduCommentService;

  @Autowired
  private UcenterClient ucenterClient;

  //根据课程id_分页查询课程评论的方法
  @GetMapping("/getCommentPage/{page}/{limit}")
  public R getCommentPage(@PathVariable Long page,@PathVariable Long limit,String courseId){
    Page<EduComment> commentPage = new Page<>(page, limit);

    QueryWrapper<EduComment> wrapper = new QueryWrapper<>();

    //判断课程id是否为空
    if (!StringUtils.isEmpty(courseId)){
      wrapper.eq("course_id",courseId);
    }
    //按最新排序
    wrapper.orderByDesc("gmt_create");

    //数据会被封装到commentPage中
    eduCommentService.page(commentPage,wrapper);

    List<EduComment> commentList = commentPage.getRecords();
    long current = commentPage.getCurrent();//当前页
    long size = commentPage.getSize();//一页记录数
    long total = commentPage.getTotal();//总记录数
    long pages = commentPage.getPages();//总页数
    boolean hasPrevious = commentPage.hasPrevious();//是否有上页
    boolean hasNext = commentPage.hasNext();//是否有下页

    HashMap<String, Object> map = new HashMap<>();
    map.put("current",current);
    map.put("size",size);
    map.put("total",total);
    map.put("pages",pages);
    map.put("hasPrevious",hasPrevious);
    map.put("hasNext",hasNext);
    map.put("list",commentList);

    return R.ok().data(map);
  }

  //添加评论
  @PostMapping("/auth/addComment")
  public R addComment(HttpServletRequest request,@RequestBody EduComment eduComment){
    String memberId = JwtUtils.getMemberIdByJwtToken(request);
    //判断用户是否登录
    if (StringUtils.isEmpty(memberId)){
      return R.error().code(20001).message("请登录");
    }
    eduComment.setMemberId(memberId);

    //System.out.println(memberId);
    //远程调用ucenter根据用户id获取用户信息
    UcenterMemberVo memberVo = ucenterClient.getMemberInfoById(memberId);

    eduComment.setAvatar(memberVo.getAvatar());
    eduComment.setNickname(memberVo.getNickname());
    //保存评论
    eduCommentService.save(eduComment);
    return R.ok();
  }
  //删除评论
  @ApiOperation(value = "逻辑删除评论")
  @DeleteMapping("/auth/delComment/{id}")
  public R delComment(@PathVariable String id){

    boolean flag = eduCommentService.removeById(id);
    if (flag){
      return R.ok();
    }else {
      return R.error();
    }
  }
  //3.分页查询讲师的方法 current -代表当前页 limit 代表每页显示的记录数
  @ApiOperation(value = "分页查询评论")
  @GetMapping("page/{current}/{limit}")
  public R pageListTeacher(@PathVariable long current,
                           @PathVariable long limit){

    //创建page对象
    Page<EduComment> pageTeacher = new Page<>(current,limit);

    QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
    wrapper.orderByDesc("gmt_create");
    //调用方法实现分页
    //调用方法时候，底层封装，把分页所有数据封装到pageTeacher对象里面
    eduCommentService.page(pageTeacher,wrapper);
    long total = pageTeacher.getTotal();//返回总记录数
    List<EduComment> records = pageTeacher.getRecords();//没页数数据list集合

    //第一中方法
  /*      Map map =new HashMap();
        map.put("total",total);
        map.put("rows",records);
        return  R.ok().data(map);*/
    //第二种方法
    return R.ok().data("total",total).data("rows",records);
  }


}


